package org.mountain.music.service;

import android.app.Service;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.IBinder;
import android.os.Messenger;
import android.support.annotation.Nullable;
import android.util.Log;

import java.io.IOException;

/**
 * Created by Administrator on 2018\9\12 0012.
 */

public class MusicService extends Service {

    MediaPlayer mediaPlayer;
    Messenger messenger;
    @Nullable
    @Override

    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public void onCreate() {


    }


    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        System.out.println("onStartCommand invoke");

        String option=intent.getStringExtra("option");
        if (messenger==null) {
            messenger = (Messenger) intent.getExtras().get("messenger");

        }
        if ("开始".equals(option)) {

            Log.e("startcommand","start music");
            start(intent.getStringExtra("path"));
        } else if ("暂停".equals(option)) {
            pause();
        } else if ("继续".equals(option)) {
          //  continuePlay();
        } else if ("停止".equals(option)) {
            stop();
        } else if ("跳转".equals(option)) {
           // seekPlay(intent.getIntExtra("progress",-1));
        }





        return super.onStartCommand(intent, flags, startId);
    }


    public void start(String path) {

        mediaPlayer = new MediaPlayer();
        mediaPlayer.reset();
        try {
            mediaPlayer.setDataSource(path);
            mediaPlayer.prepare();
            mediaPlayer.start();

        } catch (IOException e) {
            e.printStackTrace();
        }


    }


    public void pause() {
        if (mediaPlayer == null && mediaPlayer.isPlaying()) {
            mediaPlayer.pause();

        }

    }


    public void stop() {
        if (mediaPlayer == null && mediaPlayer.isPlaying()) {
            mediaPlayer.stop();

        }


    }

    public void continueplay() {

        if (mediaPlayer == null && mediaPlayer.isPlaying()) {
            mediaPlayer.start();
            mediaPlayer.start();

        }
    }
}
